layui.extend({
  admin: 'http://localhost:8080/js/admin'
});
layui.use(['form', 'layer', 'admin', 'jquery'], function () {
  var form = layui.form,
    admin = layui.admin,
    layer = layui.layer,
    $ = layui.jquery;
  //自定义验证规则
  form.verify({
    nikename: function (value) {
      if (value.length < 1) {
        return '昵称至少得1个字符';
      }
    }
    , pass: [/(.+){6,12}$/, '密码必须6到12位']
    , repass: function (value) {
      if ($('#L_pass').val() != $('#L_repass').val()) {
        return '两次密码不一致';
      }
    }
  });

  //监听提交
  form.on('submit(add)', function (data) {
    console.log("edit data :"+JSON.stringify(data));
    //发异步，把数据提交给php
      $.ajax({
          url: 'http://localhost:8080/admin/user/edit',
          type: 'POST',
          dataType: 'json',
          contentType: 'application/json;charset=UTF-8',
          data: JSON.stringify(data.field),
          success: function (res) {
              console.log('success');
              console.log(res);
              if (res.result === "SUCCESS") {
                  layer.alert("修改成功", { icon: 6 }, function () {
                      // 获得frame索引
                      var index = parent.layer.getFrameIndex(window.name);
                      //关闭当前frame
                      parent.layer.close(index);
                  });
              } else {
                  layer.msg(res.message);
              }
          }
      });
    return false;
  });

});